<!-- 地址列表 -->
<template>
	<view class="address-wrap">
		<view class="address-list" v-for="address in addressList" :key="address.id" @tap="useAddress(address)">
			<view class="top u-flex u-row-between">
				<text class="name">收货人：{{ address.consignee }}</text>
				<text class="phone">{{ address.phone }}</text>
				<!-- <text class="tag" v-show="address.is_default === '1'">默认</text> -->
			</view>
			<view class="detail">{{ address.province_name }}{{ address.city_name }}{{ address.area_name }}{{ address.address }}</view>
			<!-- <button class="u-reset-button set-btn" @tap.stop="jump('/pages/user/address/edit', { id: address.id })">编辑</button> -->
			<view class="btm u-flex">
				<image class="imgs" @tap.stop="jump('/pages/user/address/edit', { id: address.id })" :src="address.is_default=='1'?`${$IMG_URLT}address_01.png`:`${$IMG_URLT}address_02.png`"></image>
				<view class="" style="margin-right: auto;" @tap.stop="jump('/pages/user/address/edit', { id: address.id })">设为默认地址</view>
				<view class="text" @tap.stop="jump('/pages/user/address/edit', { id: address.id })">编辑</view>
				<u-line style="flex:none;" color="#111" direction="col" length="32rpx" />
				<view class="text text2" @tap.stop="deleteAddress(address.id)">删除</view>
			</view>
		</view>

		<view class="foot_box-wrap safe-area-inset-bottom">
			<view class="foot_box u-flex u-row-between safe-area-inset-bottom">
				<!-- 微信小程序和微信H5 -->
				<!-- <button
					class="u-reset-button sync-wxaddress u-m-20 u-flex u-row-center u-col-center"
					@tap="getWXaddress"
					v-show="platform == 'wxMiniProgram' || platform == 'wxOfficialAccount'"
				>
					<text class="u-iconfont uicon-weixin-fill u-p-r-10" style="color:#09bb07;font-size: 40rpx;"></text>

					导入微信地址
				</button> -->
				<button class="u-reset-button add-btn u-m-20" @tap="jump('/pages/user/address/edit')">添加新的收货地址</button>
			</view>
		</view>
	</view>
</template>

<script>
import Auth from '@/shopro/permission/index.js';
export default {
	components: {},
	data() {
		return {
			addressList: [],
			platform: this.$platform.get(),
			jumptype:1
		};
	},
	computed: {
		
	},
	onLoad() {
		this.jumptype = this.$Route.query.jumptype?this.$Route.query.jumptype:1
	},
	onShow() {
		this.getAddressList();
		// 监听地址
		// uni.$on("SELECT_ADDRESS", (res) => {
		//   if (res.addressData) {
		// 	this.addressData = JSON.parse(res.addressData);
		// 	this.addressId = this.addressData.id;
		//   } else {
		// 	this.addressId = 0;
		//   }
		//   // this.getPre();
		//   res.addressData && uni.$off("SELECT_ADDRESS");
		// });
	},
	methods: {
		// 选中
		useAddress(address) {
			console.log(address,this.jumptype)
			uni.$emit('SELECT_ADDRESS', { 'addressData': JSON.stringify(address) });
			if(this.jumptype==2){
				wx.navigateBack()
			}else{
				uni.navigateBack()
			}
			
		},
		// 路由跳转
		jump(path, parmas) {
			this.$Router.push({
				path: path,
				query: parmas
			});
		},
		// 微信导入
		getWXaddress() {
			let authState = new Auth('address').check();
			// #ifdef MP
			authState &&
				uni.chooseAddress({
					success: res => {
						this.jump('/pages/user/address/edit', { addressData: res });
					},
					fail: err => {
						console.log('%cuni.chooseAddress,调用失败', 'color:green;background:yellow');
					}
				});
			// #endif
			// #ifdef H5
			this.$wxsdk.openAddress(res => {
				this.jump('/pages/user/address/edit', { addressData: res });
			});
			// #endif
		},
		// 删除收货地址
		deleteAddress(id) {
			const that = this;
			that.$http(
				'address.del',
				{
					id: id
				},
				'删除中...'
			).then(res => {
				if (res.code === 1) {
					that.getAddressList();
				}
			});
		},
		getAddressList() {
			this.$http('address.list').then(res => {
				if (res.code === 1) {
					this.addressList = res.data;
					!this.addressList.length && uni.$emit('SELECT_ADDRESS', { addressData: null });
				}
			});
		}
	}
};
</script>

<style lang="scss">
.address-list {
	width: 702rpx;
	
	background: #FFFFFF;
	border-radius: 16rpx;
	margin:0 auto;
	margin-top:24rpx;
	padding:38rpx 24rpx;
	.name,.phone{
		font-size: 28rpx;
		font-weight: 600;
		color: #111111;
	}
	.detail{
		height: 88rpx;
		font-size: 28rpx;
		font-family: SourceHanSansCN-Regular, SourceHanSansCN;
		font-weight: 400;
		color: #555555;
		line-height: 44rpx;
		margin-top:16rpx;
		overflow:hidden;
		  text-overflow: ellipsis;
		  -webkit-line-clamp: 2;
		  display: -webkit-box;
		  -webkit-box-orient: vertical;
	}
	.btm{
		padding-top:30rpx;
		border-top:2rpx solid #E4E4E4;
		font-size: 24rpx;
		color: #111111;
		margin-top:20rpx;
		.imgs{
			width: 64rpx;
			height: 36rpx;
			margin-right: 16rpx;
		}
		.text{
			font-size: 28rpx;
			margin:0 15rpx;
		}
		.text2{
			color:#E60000;
			margin-right:0;
		}
	}
}

// 底部按钮
.foot_box-wrap {
	height: 140rpx;
	width: 100%;
}
.foot_box {
	padding: 10rpx 20rpx;
	position: fixed;
	bottom: 0;
	left: 0;
	width: 100%;
	background-color: #fff;
	border-top: 1rpx solid rgba(#ccc, 0.2);
	.sync-wxaddress {
		flex: 1;
		line-height: 80rpx;
		background: rgba(255, 255, 255, 1);
		border-radius: 40rpx;
		box-shadow: 0 0 1rpx 6rpx rgba(#ccc, 0.2);
	}
	.add-btn {
		line-height: 80rpx;
		flex: 1;
		background: #FBB03B;
		border-radius: 40rpx;
		color: #111111;
	}
}
</style>
